package tu.appengine.rest;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import tu.appengine.model.Attendance;
import tu.appengine.model.Grade;
import tu.appengine.model.Student;
import tu.appengine.model.StudentDAO;

@Path("/")
public class StudentInfo {
	
	@Path("helloWorld/{studentid}")
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String doStuff() {
		return "Hello world";
	}
	
	@Path("insert-test-student/{studentId}/{personalId}")
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String testStudent(@PathParam("studentId") String studentId, 
			@PathParam("personalId") String personalId) {
		StudentDAO dao = StudentDAO.getInstance();
		dao.insertTestStudent(studentId, personalId);
		
		return "Student added";
	}
	
	@GET
	@Path("student-info/{studentId}/{personalId}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response student(@PathParam("studentId") String studentId, 
			@PathParam("personalId") String personalId) {
		ResponseBuilder response = Response.status(200);
		response.header("Content-Type", MediaType.APPLICATION_JSON);
		response.header("access-control-allow-origin", "*");  //slujebna inf
		
		StudentDAO dao = StudentDAO.getInstance();
		Student student = dao.retrieveStudent(studentId, personalId);
		
		response.entity(student);
		
		return response.build();
	}
	
	@GET
	@Path("grades/{studentId}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response grades(@PathParam("studentId") String studentId) {
		ResponseBuilder response = Response.status(200);
		response.header("Content-Type", MediaType.APPLICATION_JSON);
		response.header("access-control-allow-origin", "*");  //slujebna inf
		
		StudentDAO dao = StudentDAO.getInstance();
		List<Grade> grades = dao.retrieveGrades(studentId);
		
		response.entity(grades);
		
		return response.build();
	}
	
	@GET
	@Path("attendances/{studentId}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response attendances(@PathParam("studentId") String studentId) {
		ResponseBuilder response = Response.status(200);
		response.header("Content-Type", MediaType.APPLICATION_JSON);
		response.header("access-control-allow-origin", "*");  //slujebna inf
		
		StudentDAO dao = StudentDAO.getInstance();
		List<Attendance> attendances = dao.retrieveAttendances(studentId);
		
		response.entity(attendances);
		
		return response.build();
	}
}
